<?php 
/**
*	文件下载预览
*/
class fileClassAction extends ActionNot
{
	public $userinfo = array();
	
	public function initAction()
	{
		$key = $this->get('key');
		if(isempt($key))exit('url is error1');
		$da  = c('cache')->get($key);
		if(!$da)exit('url is wuxiao');
		$fileid  = $da['fileid'];
		$websign = $da['websign'];
		$this->userinfo = $da['userinfo'];
		if(md5($this->rock->HTTPweb)!=$websign)exit('url is error2');

		$this->frs 		= m('file')->getone($fileid);
		$this->fileid 	= $fileid;
		if(!$this->frs)exit('record not found');
		$filepath = $this->frs['filepath'];
		if(!file_exists($filepath))exit('file not found');
	}
	
	/**
	*	预览文件
	*/
	public function defaultAction()
	{
		$fileext	= $this->frs['fileext'];
		$pdfpath	= $this->frs['pdfpath'];
		$this->assign('frs', $this->frs);
		if($fileext=='pdf'){
			return $this->view_pdf($this->frs['filepath']);
		}else{
			if(!isempt($pdfpath) && file_exists($pdfpath))return $this->view_pdf($pdfpath);
		}
		$exiss  = ','.$fileext.',';
		$office = ',docx,doc,xls,xlsx,ppt,pptx,';
		$imgext = ',gif,png,jpeg,jpg,';
		
		if(contain($office, $exiss)){
			$this->view_office();
		}else if(contain($imgext, $exiss)){
			$this->view_image();
		}else if($fileext=='mp4'){	
			$this->view_mp4();
		}else{
			$this->view_open();
		}
	}
	private function view_pdf($filepath){
		$this->displayfile = ''.P.'/file/view_pdf.html';
		$this->assign('filepath', $this->jm->base64encode($filepath));
	}
	private function view_open(){
		$this->displayfile = ''.P.'/file/view_open.html';
		$content = '';
		$filepath	= $this->frs['filepath'];
		$content	= file_get_contents($filepath);
		if(substr($filepath,-4)=='temp'){
			$content	= base64_decode($content);
		}
		$this->smartydata['content']=$content;
	}
	private function view_image(){
		$this->displayfile = ''.P.'/file/view_image.html';
	}
	private function view_mp4(){
		$this->displayfile = ''.P.'/file/view_mp4.html';
	}
	private function view_office(){
		$fan = getconfig('officebj_fan');
		if($fan==2 || $fan==3){
			$filepath	= $this->rock->getouturl().$this->frs['filepath'];
			$url = 'https://view.officeapps.live.com/op/view.aspx?src='.urlencode($filepath).'';
			if($fan==3)$url = 'https://docview.mingdao.com/op/view.aspx?src='.urlencode($filepath).'';
			$this->rock->location($url);
		}else if($fan==4){
			$this->displayfile = ''.P.'/file/view_topdf.html';
			$this->smartydata['key']=$this->get('key');
		}else{
			$this->displayfile = ''.P.'/file/view_office.html';
			$this->smartydata['key']=$this->get('key');
		}
	}
	
	
	/**
	*	下载
	*/
	public function downAction()
	{
		$this->display = false;
		m('file')->update('`downci`=`downci`+1', $this->fileid);
		$size		= $this->frs['filesize'];
		$filename	= $this->frs['filename'];
		$fileext	= $this->frs['fileext'];
		$filepath	= $this->frs['filepath'];
	
		$filename 	= $this->iconvutf8(str_replace(' ','',$filename));
		header('Content-type:application/octet-stream');
		header('Accept-Ranges: bytes');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: no-cache');
		if($size>0)header('Content-Length:'.$size.'');
		header('Expires: 0');
		header('Content-disposition:attachment;filename='.$filename.'');
		header('Content-Transfer-Encoding: binary');
		ob_clean();
		flush();
		if(substr($filepath,-4)=='temp'){
			$content	= file_get_contents($filepath);
			return base64_decode($content);
		}else{
			readfile($filepath);
		}
	}
	
	//渣渣IE才需要转化，真是醉了
	private function iconvutf8($text) {
		if(contain($this->rock->web,'IE')){
			return iconv('utf-8','gb2312', $text);
		}else{
			return $text;
		} 
    }
	
	
	
	
	
	
	
	
	
	/**
	*	在线编辑平台发送预览
	*/
	public function sendeditAction()
	{
		$otype = (int)$this->get('otype','0');
		return c('rockedit')->sendedit($this->fileid,$this->userinfo,1);
	}
	
	/**
	*	请求转化
	*/
	public function changetopdfAction()
	{
		return c('xinhuapi')->officesend($this->fileid);
	}
	
	/**
	*	获取状态
	*/
	public function officestatusAction()
	{
		return c('xinhuapi')->officestatus($this->fileid);
	}
	
	/**
	*	下载文件
	*/
	public function officedownAction()
	{
		return c('xinhuapi')->officedown($this->fileid);
	}

}